/*
 * @Author: i@rysa.cn
 * @Date: 2022-05-14 17:00:53
 * @LastEditTime: 2022-06-01 09:58:48
 * @LastEditors: i@rysa.cn
 * @Description:
 * @FilePath: \go-utils\wechat\pay\tools.go
 */
package pay

import (
	"context"

	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
)

func GetClient(cfg *PayConfig) (*core.Client, context.Context, error) {
	ctx := context.Background()
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath(cfg.ApiclientKey)
	if err != nil {
		return nil, ctx, err
	}
	// 使用商户私钥等初始化 client，并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(cfg.MchId, cfg.MchCertificateSerialNumber, mchPrivateKey, cfg.MchApiV3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		return nil, ctx, err
	}
	return client, ctx, nil
}
